package com.bl.student.service.biz.information.impl;

import com.bl.student.common.ApiResponseCode;
import com.bl.student.common.CommonServiceException;
import com.bl.student.common.enums.InformationType;
import com.bl.student.common.enums.RecommendType;
import com.bl.student.common.enums.ValidStatus;
import com.bl.student.common.utils.ListUtils;
import com.bl.student.common.utils.UUIDUtils;
import com.bl.student.dao.model.*;
import com.bl.student.dao.repository.*;
import com.bl.student.service.biz.information.InformationService;
import com.bl.student.service.vo.DelBody;
import com.bl.student.service.vo.DelListBody;
import com.bl.student.service.vo.information.*;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by liangweikang on 18/1/20.
 */
@Service
@Log4j2
public class InformationServiceImpl implements InformationService{

    @Autowired
    InformationRepository informationRespository;

    @Autowired
    RecommendRepository recommendRepository;

    @Autowired
    DetailRespository detailRespository;

    @Autowired
    VipRepository vipRepository;

    @Autowired
    TeacherStudentRepository teacherStudentRespository;

    @Autowired
    StudioRespository studioRespository;


    @Override
    public InformationListVo getInformationList(InformationType type, Pageable pageable) {
        InformationListVo informationListVo = new InformationListVo();

        Page<InformationEntity> informationEntities;

        informationEntities = informationRespository.findByTypeOrderByWeightDesc(type,pageable);

        informationListVo.setTotalCount(informationEntities.getTotalElements());
        informationListVo.setList(informationEntities.getContent());

        return informationListVo;
    }

    @Override
    public DetailEntity getInformationEntity(String id) {
        DetailEntity informationEntity = new DetailEntity();

        informationEntity = detailRespository.findOne(id);

        return informationEntity;
    }

    @Override
    public HomePageVo getHomePage(String id) {
        Pageable pageable = new PageRequest(0, 6);

        HomePageVo homePageVo = new HomePageVo();

        Page<InformationEntity> informationEntities;
        Page<RecommendEntity> recommendEntities;
        informationEntities = informationRespository.findByTypeOrderByWeightDesc(InformationType.CAMPUS, pageable);
        if (informationEntities != null)
            homePageVo.setCampusList(informationEntities.getContent());

        informationEntities = informationRespository.findByTypeOrderByWeightDesc(InformationType.PLAN, pageable);
        if (informationEntities != null)
            homePageVo.setPlanList(informationEntities.getContent());

        informationEntities = informationRespository.findByTypeOrderByWeightDesc(InformationType.FAVORITE, pageable);
        if (informationEntities != null)
            homePageVo.setFavoriteList(informationEntities.getContent());

        informationEntities = informationRespository.findByTypeOrderByWeightDesc(InformationType.EXPERIENCE, pageable);
        if (informationEntities != null)
            homePageVo.setExpList(informationEntities.getContent());

        recommendEntities = recommendRepository.findByTypeOrderByWeightDesc(RecommendType.INDEX_CAROUSEL,pageable);
        if (recommendEntities != null)
            homePageVo.setIndexLunboList(recommendEntities.getContent());

        List<TeacherStudentEntity> teacherStudentEntities = teacherStudentRespository.findByStudentId(id);

        if (teacherStudentEntities.size() != 0){
            homePageVo.setIsHasTeacher(true);
        } else
            homePageVo.setIsHasTeacher(false);

        List<VipEntity> vipEntityList =  vipRepository.findByUserId(id);
        VipEntity vipEntity = ListUtils.getFirst(vipEntityList);

        if (vipEntity != null && vipEntity.getStatus() == ValidStatus.VALID){
            homePageVo.setIsVIP(true);
        } else
            homePageVo.setIsVIP(false);

        return homePageVo;

    }

    @Override
    public PlanLunboListVo getArtLunboList() {
        PlanLunboListVo planLunboListVo = new PlanLunboListVo();

        List<RecommendEntity> recommendEntities;

        recommendEntities = recommendRepository.findAllByTypeOrderByWeightDesc(RecommendType.PLAN_CAROUSEL);

        planLunboListVo.setList(recommendEntities);

        return planLunboListVo;
    }

    @Override
    public PlanLunboListVo getArtLunboManagerList() {
        PlanLunboListVo planLunboListVo = new PlanLunboListVo();

        List<RecommendEntity> recommendEntities;

        recommendEntities = recommendRepository.findAllByTypeOrderByCreateTimeDesc(RecommendType.PLAN_CAROUSEL);

        planLunboListVo.setList(recommendEntities);

        return planLunboListVo;
    }

    @Override
    public void del(DelBody id) {
        informationRespository.delete(id.getId());
    }

    @Override
    public void delList(DelListBody idList) {
        for(String id : idList.getList()) {

            informationRespository.delete(id);

        }
    }

    @Override
    public void delLunbo(DelBody id) {
        recommendRepository.delete(id.getId());
    }

    @Override
    public void addLunbo(PlanLunboAddVo planLunboAddVo) {

        RecommendEntity recommendEntity = new RecommendEntity();
        recommendEntity.setCover(planLunboAddVo.getCover());
        recommendEntity.setCreateTime(new Date());
        recommendEntity.setDescription(planLunboAddVo.getDescription());
        recommendEntity.setDetailId(planLunboAddVo.getDetailId());
        recommendEntity.setId(UUIDUtils.getUUID());
        recommendEntity.setWeight(planLunboAddVo.getWeight());
        recommendEntity.setUpdateTime(new Date());
        recommendEntity.setTitle(planLunboAddVo.getTitle());
        recommendEntity.setType(planLunboAddVo.getType());

        recommendRepository.save(recommendEntity);

    }

    @Override
    public void modifyLunbo(PlanLunboModVo planLunboModVo) {
        RecommendEntity recommendEntity = recommendRepository.findOne(planLunboModVo.getId());

        if (recommendEntity == null) {
            throw new CommonServiceException(ApiResponseCode.DATA_INCORRECT.getCode(), "没有对应艺考信息");
        }

        recommendEntity.setCover(planLunboModVo.getCover());
        recommendEntity.setCreateTime(planLunboModVo.getCreateTime());
        recommendEntity.setDescription(planLunboModVo.getDescription());
        recommendEntity.setDetailId(planLunboModVo.getDetailId());
        recommendEntity.setId(planLunboModVo.getId());
        recommendEntity.setWeight(planLunboModVo.getWeight());
        recommendEntity.setUpdateTime(new Date());
        recommendEntity.setTitle(planLunboModVo.getTitle());
        recommendEntity.setType(planLunboModVo.getType());

        recommendRepository.save(recommendEntity);
    }

    @Override
    public void add(InformationAddVo informationAddVo) {

        InformationEntity informationEntity = new InformationEntity();
        informationEntity.setCover(informationAddVo.getCover());
        informationEntity.setCreateTime(new Date());
        informationEntity.setDescription(informationAddVo.getDescription());
        informationEntity.setDetailId(informationAddVo.getDetailId());
        informationEntity.setId(UUIDUtils.getUUID());
        informationEntity.setWeight(informationAddVo.getWeight());
        informationEntity.setUpdateTime(new Date());
        informationEntity.setType(informationAddVo.getType());
        informationEntity.setTitle(informationAddVo.getTitle());
        informationEntity.setTags(informationAddVo.getTags());
        informationRespository.save(informationEntity);

    }

    @Override
    public void modify(InformationModVo informationModVo) {

        InformationEntity informationEntity = informationRespository.findOne(informationModVo.getId());

        if (informationEntity == null) {
            throw new CommonServiceException(ApiResponseCode.DATA_INCORRECT.getCode(), "没有对应艺考信息");
        }

        informationEntity.setCover(informationModVo.getCover());
        informationEntity.setDescription(informationModVo.getDescription());
        informationEntity.setDetailId(informationModVo.getDetailId());
        informationEntity.setId(informationModVo.getId());
        informationEntity.setWeight(informationModVo.getWeight());
        informationEntity.setUpdateTime(new Date());
        informationEntity.setType(informationModVo.getType());
        informationEntity.setTitle(informationModVo.getTitle());
        informationEntity.setTags(informationModVo.getTags());
        informationRespository.save(informationEntity);

    }

    @Override
    public PlanLunboListVo getIndexLunboList() {

        PlanLunboListVo planLunboListVo = new PlanLunboListVo();

        List<RecommendEntity> recommendEntities =  recommendRepository.findAllByTypeOrderByCreateTimeDesc(RecommendType.INDEX_CAROUSEL);

        if (recommendEntities != null) {
            planLunboListVo.setList(recommendEntities);
        }
        return planLunboListVo;
    }

    @Override
    public InformationManagerListVo getInformationManagerList(InformationType type, Pageable pageable) {
        InformationManagerListVo informationManagerListVo = new InformationManagerListVo();

        Page<InformationEntity> informationEntities;

        List<InformationManagerVo> informationManagerVos = new ArrayList<>();

        informationEntities = informationRespository.findByTypeOrderByCreateTimeDesc(type,pageable);

        informationManagerListVo.setTotalCount(informationEntities.getTotalElements());

        for (InformationEntity informationEntity : informationEntities) {
            InformationManagerVo informationManagerVo = new InformationManagerVo();
            informationManagerVo.setCover(informationEntity.getCover());
            informationManagerVo.setCreateTime(informationEntity.getCreateTime());
            informationManagerVo.setDescription(informationEntity.getDescription());
            informationManagerVo.setDetailId(informationEntity.getDetailId());
            informationManagerVo.setId(informationEntity.getId());
            informationManagerVo.setTitle(informationEntity.getTitle());
            informationManagerVo.setType(informationEntity.getType());
            informationManagerVo.setUpdateTime(informationEntity.getUpdateTime());
            informationManagerVo.setWeight(informationEntity.getWeight());

            informationManagerVo.setTags(informationEntity.getTags());

            informationManagerVo.setCount(studioRespository.countByBizId(informationEntity.getId()));
            informationManagerVos.add(informationManagerVo);
        }
        informationManagerListVo.setList(informationManagerVos);

        return informationManagerListVo;
    }

    @Override
    public PlanLunboListVo getPlanLunboList() {

        PlanLunboListVo planLunboListVo = new PlanLunboListVo();

        List<RecommendEntity> recommendEntities;

        recommendEntities = recommendRepository.findAllByTypeOrderByWeightDesc(RecommendType.PLAN_CAROUSEL);

        if (recommendEntities != null) {
            planLunboListVo.setList(recommendEntities);
        }

        return planLunboListVo;
    }

    @Override
    public PlanLunboListVo getPlanLunboManagerList() {

        PlanLunboListVo planLunboListVo = new PlanLunboListVo();

        List<RecommendEntity> recommendEntities;

        recommendEntities = recommendRepository.findAllByTypeOrderByCreateTimeDesc(RecommendType.PLAN_CAROUSEL);

        if (recommendEntities != null) {
            planLunboListVo.setList(recommendEntities);
        }

        return planLunboListVo;
    }
}
